package dao.impl;

import dao.MessageDao;
import domian.InformMessage;
import domian.Message;
import domian.OfficalMessage;
import utils.BaseDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MessageDaoImpl implements MessageDao {
    @Override
    public List<Message> findAllMessage() {
        List<Message> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;
        String sql = "select *from message";
        try {
            connection = BaseDAO.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            Message message = null;
            while (resultSet.next()){
                Integer id1 = resultSet.getInt("id");
                Integer uid = resultSet.getInt("uid");
                String mOffical = resultSet.getString("m_offical");
                String mInform = resultSet.getString("m_inform");
                String mPrivate = resultSet.getString("m_private");
                String friendId = resultSet.getString("friend_id");
                String blacklist = resultSet.getString("blacklist");
                message =new Message(id1,uid,mOffical,mInform,mPrivate,friendId,blacklist);
                list.add(message);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDAO.close(connection,preparedStatement,resultSet);
        }

        return list;
    }

    @Override
    public Message findMessageById(Integer uid) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;
        Message message =null;
        String sql = "select * from message where uid=?";
        try {
            connection = BaseDAO.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,uid);
            resultSet = preparedStatement.executeQuery();
            InformMessage InformMessage = null;
            while (resultSet.next()){
                Integer mid = resultSet.getInt("uid");
                String m_offical = resultSet.getString("m_offical");
                String m_inform = resultSet.getString("m_inform");
                String m_private = resultSet.getString("m_private");
                String friend_id = resultSet.getString("friend_id");
                String blacklist = resultSet.getString("blacklist");
                message = new Message(mid, m_offical, m_inform, m_private, friend_id, blacklist);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDAO.close(connection,preparedStatement,resultSet);
        }
        return message;
    }

    @Override
    public Integer updateMessage(Message message) {
        String sql ="update message set m_offical=?, m_inform=?, m_private=?, friend_id=?, blacklist=? where uid=?";
        Object[] arr={message.getM_offical(),message.getM_inform(),message.getM_private(),message.getFriend_id(),message.getBlacklist(),message.getUid()};
        return BaseDAO.executeUpdate(sql,arr);
    }

    @Override
    public Integer addMessage(Message message) {
        String sql ="insert into  message(uid,m_offical,m_inform,m_private,friend_id,blacklist) values(?,?,?,?,?,?)";
        Object[] arr={message.getUid(),message.getM_offical(),message.getM_inform(),message.getM_private(),message.getFriend_id(),message.getBlacklist()};
        return BaseDAO.executeUpdate(sql,arr);
    }

    @Override
    public List<InformMessage> findAllInformMessage() {
        List<InformMessage> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;
        String sql = "select * from m_inform ";
        try {
            connection = BaseDAO.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            InformMessage InformMessage = null;
            while (resultSet.next()){
                Integer mid = resultSet.getInt("mid");
                int anInt = resultSet.getInt("uid");
                String message = resultSet.getString("message");
                String date = resultSet.getString("date");
                InformMessage=  new InformMessage(mid,anInt,message,date);
                list.add(InformMessage);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDAO.close(connection,preparedStatement,resultSet);
        }
        return list;
    }

    @Override
    public List<OfficalMessage> findOfficalMessage() {
        List<OfficalMessage> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;
        String sql = "select * from m_offical ";
        try {
            connection = BaseDAO.getConnection();
            preparedStatement = connection.prepareStatement(sql);
             resultSet = preparedStatement.executeQuery();
            OfficalMessage officalMessage = null;
            while (resultSet.next()){
                Integer mid = resultSet.getInt("mid");
                String message = resultSet.getString("message");
                String time = resultSet.getString("time");
                officalMessage=  new OfficalMessage(mid,message,time);
                list.add(officalMessage);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDAO.close(connection,preparedStatement,resultSet);
        }

        return list;
    }
}
